package Tracker.server.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Track extends JSONObject {
	public final int id;
	public final User owner;
	ArrayList<Point> points;
	String name;
	String location;
	
	public Track(int id, User owner, String name, String loc) throws JSONException {
		this.id = id;
		this.put("id", id);
		
		this.owner = owner;
		this.put("owner", owner);
		
		this.name = name;
		this.put("name", name);
		
		this.location = loc;
		this.put("location", loc);
		
		this.points = new ArrayList<Point>();
		this.put("points", new JSONArray());
	}
	
	public void setName(String n) throws JSONException {
		this.name = n;
		this.put("name", n);
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setLocation(String l) throws JSONException {
		this.location = l;
		this.put("location", l);
	}
	
	public String getLocation() {
		return this.location;
	}
	
	public void addPoint(Point p) throws JSONException {
		this.points.add(p);
		this.append("points", p);
	}
	
	public void addPoint(Point p, int n) throws JSONException {
		this.points.add(n, p);
		this.getJSONArray("points").put(n, p);
	}
	
	public void setPoints(ArrayList<Point> p) throws JSONException {
		this.points = p;
		JSONArray points = new JSONArray();
		
		for(int i = 0; i < this.points.size(); i++) {
			points.put(this.points.get(i));
		}
		
		this.put("points", points);
	}
	
	public Point getPoint(int n) {
		return this.points.get(n);
	}
	
	public void removePoint(int n) throws JSONException {
		this.points.remove(n);
		((JSONArray) this.get("points")).remove(n);
	}
}
